import { Attribute } from "../etc/Attribute";
import { util } from "../etc/Util";
import { CDTimer } from "../gameEtc/CDTimer";
import { LevelModuleC } from "../module/LevelModule";
import { PlayerModuleC, PlayerModuleData } from "../module/PlayerModule";
import MainUI_Generate from "../ui-generate/MainUI_generate";
import { GrowWindow } from "./GrowWindow";

export class MainUI extends MainUI_Generate{
    private pressed:boolean = false
    private forceValue = 0

    private resetCD:CDTimer = new CDTimer(1e4)

    private playerMD:PlayerModuleC
    private playerData:PlayerModuleData
    private levelMD:LevelModuleC

    onStart(){
        this.playerMD = ModuleService.getModule(PlayerModuleC)
        this.levelMD = ModuleService.getModule(LevelModuleC)
        this.playerData = DataCenterC.getData(PlayerModuleData)

        this.playerMD.onAddForceClear.add(this.onAddForceClear, this)
        this.playerData.onDataChange.add(this.onPlayerDataChanged, this)
        
        this.btn_func.onPressed.add(this.onBtnFuncPressed.bind(this))
        this.btn_func.onReleased.add(this.onBtnFuncReleased.bind(this))

        this.btn_sprint.onPressed.add(this.onSprintClicked.bind(this))
        this.btn_reset_position.onPressed.add(this.onBtnResitPositionClicked.bind(this))
        this.btn_grow.onPressed.add(()=>{
            UIService.show(GrowWindow)
        })

        this.onAddForceClear()
        this.onPlayerDataChanged()

        this.canUpdate = true
    }

    onShow(){
        this.sprintCDUpdate()
        this.resetCDUpdate()
    }

    onUpdate(){
        this.injectForceUpdate()

        this.sprintCDUpdate()
        this.resetCDUpdate()
    }

    private injectForceUpdate(){
        if(!this.pressed) return
        let addition = this.playerMD.getAttr(Attribute.EnumAttributeType.extraJumpForceGatherTimeSubtract)
        let normalPressedDelta = 0.03
        let finalDelta = ((addition / 100) * normalPressedDelta) + normalPressedDelta
        this.forceValue = util.clamp(0,1,this.forceValue + finalDelta)
        this.bar_force.currentValue = this.forceValue
    }

    private onPlayerDataChanged(){
        this.txt_money.text = `${this.playerMD.getAttr(Attribute.EnumAttributeType.money)}`
    }

    private sprintCDUpdate(){ // TODO 性能优化
        if(this.playerMD.sprintCD.getLeftTime() <= 0) {
            this.btn_sprint_mask.fanShapedValue = 1
            return
        }
        this.btn_sprint_mask.fanShapedValue = 1 - this.playerMD.sprintCD.getLeftTime() / this.playerMD.sprintCD.getCD()
    }

    private resetCDUpdate(){ // TODO 性能优化
        if(this.resetCD.getLeftTime() <= 0) {
            this.btn_reset_position_mask.fanShapedValue = 1
            return
        }
        this.btn_reset_position_mask.fanShapedValue = 1 - this.resetCD.getLeftTime() / this.resetCD.getCD()
    }

    onBtnFuncClicked() {
        Player.localPlayer.character.jump()
    }

    onBtnResitPositionClicked() {
        if(!this.resetCD.canUse()) return
        this.levelMD.resetPlayerPositionToCurrentCheckpointStartPosition()
        this.playerMD.clearPlayerDirectionalImpulse()
    }

    onBtnFuncPressed() {
        this.pressed = true
    }
    
    onBtnFuncReleased() {
        this.pressed = false
        this.playerMD.injectForce(this.forceValue)
    }

    onSprintClicked() {
        this.playerMD.sprint()
    }
    
    onAddForceClear() {
        this.forceValue = 0
        this.bar_force.currentValue = this.forceValue
    }
}